{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\Administrator\\Desktop\\12.5.ipynb 单元格 1\u001b[0m line \u001b[0;36m5\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/Administrator/Desktop/12.5.ipynb#W0sZmlsZQ%3D%3D?line=54'>55</a>\u001b[0m buttonCapture\u001b[39m=\u001b[39mtkinter\u001b[39m.\u001b[39mButton(root,text\u001b[39m=\u001b[39m\u001b[39m'\u001b[39m\u001b[39m取色\u001b[39m\u001b[39m'\u001b[39m,command\u001b[39m=\u001b[39mButtonCaptureClick)\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/Administrator/Desktop/12.5.ipynb#W0sZmlsZQ%3D%3D?line=55'>56</a>\u001b[0m buttonCapture\u001b[39m.\u001b[39mplace(x\u001b[39m=\u001b[39m\u001b[39m10\u001b[39m,y\u001b[39m=\u001b[39m\u001b[39m10\u001b[39m,width\u001b[39m=\u001b[39m\u001b[39m80\u001b[39m,height\u001b[39m=\u001b[39m\u001b[39m30\u001b[39m)\n\u001b[1;32m---> <a href='vscode-notebook-cell:/c%3A/Users/Administrator/Desktop/12.5.ipynb#W0sZmlsZQ%3D%3D?line=57'>58</a>\u001b[0m root\u001b[39m.\u001b[39;49mmainloop()\n",
      "File \u001b[1;32mc:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\tkinter\\__init__.py:1504\u001b[0m, in \u001b[0;36mMisc.mainloop\u001b[1;34m(self, n)\u001b[0m\n\u001b[0;32m   1502\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mmainloop\u001b[39m(\u001b[39mself\u001b[39m, n\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m):\n\u001b[0;32m   1503\u001b[0m \u001b[39m    \u001b[39m\u001b[39m\"\"\"Call the mainloop of Tk.\"\"\"\u001b[39;00m\n\u001b[1;32m-> 1504\u001b[0m     \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mtk\u001b[39m.\u001b[39;49mmainloop(n)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。请查看单元格中的代码，以确定故障的可能原因。有关详细信息，请单击 <a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>。有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "import tkinter\n",
    "import tkinter.filedialog\n",
    "import tkinter.messagebox\n",
    "import os\n",
    "from PIL import ImageGrab,Image\n",
    "from time import sleep\n",
    "\n",
    "root=tkinter.Tk()\n",
    "root.geometry('100x40+400+300')\n",
    "root.resizable(False,False)\n",
    "\n",
    "class MyCapture:\n",
    "    def __init__(self,png):\n",
    "        self.X=tkinter.IntVar(value=0)\n",
    "        self.Y=tkinter.IntVar(value=0)\n",
    "        \n",
    "        screen_width=root.winfo_screenwidth()\n",
    "        screen_height=root.winfo_screenheight()\n",
    "\n",
    "        self.top=tkinter.Toplevel(root,\n",
    "        width=screen_width,height=screen_height)\n",
    "        self.top.overrideredirect(True)\n",
    "        self.canvas=tkinter.Canvas(self.top,bg='white',\n",
    "        width=screen_width,height=screen_height)\n",
    "        self.image=thinter.PhotoImage(file=png)\n",
    "        self.canvas.creat_image(screenWidth//2,screenHeight//2,image=self.image)\n",
    "\n",
    "        def onLeftButtonUp(event):\n",
    "            im=image.open(png)\n",
    "            color=im.getpixel((event.x,event.y)) [:3]\n",
    "            msg=str(color)+'\\n'\n",
    "            color='#'+''.join(map(lambda x:hex(x)[2:],color))\n",
    "            msg+=color\n",
    "            tkinter.messagebox.showinfo('',msg)\n",
    "\n",
    "            self.top.destroy()\n",
    "            self.canvas.bind('<Button-1>',onLeftButtonUp)\n",
    "          \n",
    "            self.canvas.pack(fill=tkinter.BOTH,expand=tkinter.YES)\n",
    "\n",
    "def ButtonCaptureClick():\n",
    "    root.state('icon')\n",
    "    sleep(0.2)\n",
    "    \n",
    "    filename='temp.png'\n",
    "    im=ImageGrab.grab()\n",
    "    im.save(filename)\n",
    "    im.close()\n",
    "\n",
    "    w=MyCapture(filename)\n",
    "    ButtonCapture.wait_window(w.top)\n",
    "\n",
    "    root.state('normal')\n",
    "    os.remove(filename)\n",
    "buttonCapture=tkinter.Button(root,text='取色',command=ButtonCaptureClick)\n",
    "buttonCapture.place(x=10,y=10,width=80,height=30)\n",
    "\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
